<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Compare two Unicode strings</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="collator.asort.html">« Collator::asort</a></li>
      <li style="float: right;"><a href="collator.construct.html">Collator::__construct »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.collator.html">Collator</a></li>
    <li>Compare two Unicode strings</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="collator.compare" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Collator::compare</h1>
  <h1 class="refname">collator_compare</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8, PECL intl &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">Collator::compare</span> -- <span class="refname">collator_compare</span> &mdash; <span class="dc-title">Compare two Unicode strings</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-collator.compare-description">
  <h3 class="title">说明</h3>
  <p class="para">
   面向对象风格
  </p>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Collator::compare</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$string1</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$string2</code></span>): <span class="type"><span class="type">int</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   过程化风格
  </p>
  <div class="methodsynopsis dc-description"><span class="methodname"><strong>collator_compare</strong></span>(<span class="methodparam"><span class="type"><a href="class.collator.html" class="type Collator">Collator</a></span> <code class="parameter">$object</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$string1</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$string2</code></span>): <span class="type"><span class="type">int</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   Compare two Unicode strings according to collation rules.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-collator.compare-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">object</code></dt>

     <dd>

      <p class="para">
       <span class="classname"><a href="class.collator.html" class="classname">Collator</a></span> object.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">string1</code></dt>

     <dd>

      <p class="para">
       The first string to compare.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">string2</code></dt>

     <dd>

      <p class="para">
       The second string to compare.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-collator.compare-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">Return comparison result:</p>
  <p class="para">
   <ul class="itemizedlist">
    <li class="listitem">
     <p class="para">
      1 if <code class="parameter">string1</code> is <em class="emphasis">greater</em> than 
      <code class="parameter">string2</code> ;
     </p>
    </li>
    <li class="listitem">
     <p class="para">
      0 if <code class="parameter">string1</code> is <em class="emphasis">equal</em> to 
      <code class="parameter">string2</code>;
     </p>
    </li>
    <li class="listitem">
     <p class="para">
      -1 if <code class="parameter">string1</code> is <em class="emphasis">less</em> than 
      <code class="parameter">string2</code> .
     </p>
    </li>
   </ul>
   Returns <strong><code>false</code></strong> on failure.
  </p>
  <div class="warning"><strong class="warning">警告</strong><p class="simpara">此函数可能返回布尔值
<strong><code>false</code></strong>，但也可能返回等同于 <strong><code>false</code></strong> 的非布尔值。请阅读 <a href="language.types.boolean.html" class="link">布尔类型</a>章节以获取更多信息。应使用
<a href="language.operators.comparison.html" class="link">===
运算符</a>来测试此函数的返回值。</p></div>
 </div>


 <div class="refsect1 examples" id="refsect1-collator.compare-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2282">
    <p><strong>示例 #1 <span class="function"><strong>collator_compare()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$s1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Hello'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$s2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$coll&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">collator_create</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'en_US'&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$res&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">collator_compare</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$coll</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$s1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$s2&nbsp;</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">collator_get_error_message</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$coll&nbsp;</span><span style="color: #007700">);<br />}&nbsp;else&nbsp;if(&nbsp;</span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"s1&nbsp;is&nbsp;greater&nbsp;than&nbsp;s2\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;if(&nbsp;</span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"s1&nbsp;is&nbsp;less&nbsp;than&nbsp;s2\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"s1&nbsp;is&nbsp;equal&nbsp;to&nbsp;s2\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen"><br />
     s1 is greater than s2<br />
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2283">
    <p><strong>示例 #2 Comparing strings without diacritics or case-sensitivity</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$c&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Collator</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'en'&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setStrength</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">Collator</span><span style="color: #007700">::</span><span style="color: #0000BB">PRIMARY&nbsp;</span><span style="color: #007700">);<br /><br />if&nbsp;(&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">compare</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'Séan'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Sean'&nbsp;</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;same\n"</span><span style="color: #007700">;<br />}</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen"><br />
     The same<br />
    </div>
    <div class="example-contents"><p>
     This example instructs the collator to compare with only taking the base
     characters into account. The documentation for
     <span class="function"><a href="collator.setstrength.html" class="function">Collator->setStrength()</a></span> explains the different
     strengths.
    </p></div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-collator.compare-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="collator.sort.html" class="function" rel="rdfs-seeAlso">collator_sort()</a> - Sort array using specified collator</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>